package com.link.base.base.coupons.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.link.base.user.model.CoreUser;
import com.link.base.base.coupons.model.*;
import com.link.base.base.user.model.User;
import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.model.AppOrderInfo;
import com.link.base.base.accnt.service.AccountService;
import com.link.base.base.coupons.model.*;
import com.link.base.base.coupons.service.CollectRecordService;
import com.link.base.base.coupons.service.CoupOrgService;
import com.link.base.base.coupons.service.CoupStoreService;
import com.link.base.base.coupons.service.CouponsService;
import com.link.base.base.product.model.Product;
import com.link.base.base.product.service.ProductService;
import com.link.base.base.saleactivity.model.SaleActivity;
import com.link.base.base.saleactivity.service.SaleActivityService;
import com.link.base.base.user.service.UserService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 汪大伟
 * @date 2018/6/10
 * <p>优惠券Controller</p>
 * 主要方法：优惠卷新建，优惠卷编辑，优惠卷核销（使用），优惠卷领取，app端可用优惠卷筛选，优惠卷列表展示等。
 */
@Controller
@RequestMapping("/link/coupons")
public class CouponsController extends BasicController<Coupons> {

    @Resource
    private CouponsService couponsService;

    @Resource
    private AccountService accountService;

    @Resource
    private CoupStoreService coupStoreService;

    @Resource
    private CoupOrgService coupOrgService;

    @Resource
    private CollectRecordService collectRecordService;

    @Resource
    private SaleActivityService saleActivityService;

    @Resource
    private UserService userService;

    @Resource
    private ProductService productService;

    @Override
    public BasicService<Coupons> getBasicService() throws Exception {
        return couponsService;
    }

    /**
     * 优惠卷列表, 筛选
     * @author: 汪大伟
     * @date: 2018/5/19
     */
    @RequestMapping(value = "/queryCouponsListByExamplePage")
    @ResponseBody
    public Map<String, Object> queryCouponsListByExamplePage(@JsonParam QueryParams qps,@JsonParam Coupons entity,
                                                             HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            CoreUser user = UserUtil.getUser();
            Long accntId = user.getAcctId();
            Account acct = new Account();
            acct.setId(accntId);
            Account accnt = accountService.queryById(acct);
            String acctLevel = accnt.getAcctLevel();
            entity.setAccntId(accntId);
            if (accnt != null) {
                if (StringUtils.isNotBlank(acctLevel)) {
                    entity.setStoreLevel(acctLevel);
                }
                if (StringUtils.isNotBlank(accnt.getAcctChileType())) {
                    entity.setStoreType(accnt.getAcctChileType());
                }
            }
            if (null != accntId) {
                entity.setAccntId(accntId);
            }
            Long orgId = accnt.getOrgId();
            if (null != orgId) {
                entity.setOrgId(orgId);
            }
            qps.preDealSecurity(request);
            Coupons t = (Coupons) BasicModel.transformClass(entity, qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            List<Coupons> list = new ArrayList<Coupons>();
            /*
             * 渠道商登录
             * */
            if (accnt != null) {
                String dealer = "Dealer";
                String dealerEmployee = "DealerEmployee";
                if (dealer.equals(accnt.getAcctType()) && dealerEmployee.equals(user.getEmpType())) {
                    list = couponsService.queryCeCoupListByExamplePage(t);
                }
                /*
                 * 门店商登录
                 * */
                String terminal = "Terminal";
                String terminalEmployee = "TerminalEmployee";
                if (terminal.equals(accnt.getAcctType()) && terminalEmployee.equals(user.getEmpType())) {
                    list = couponsService.queryCoupListByExamplePage(t);
                }
            }
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<SaleActivity>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<SaleActivity>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 优惠卷列表查看适用门店
     * @author: 汪大伟
     * @date: 2018/5/19
     */
    @RequestMapping(value = "/querySuitAcctInfoListPage")
    @ResponseBody
    public Map<String, Object> querySuitAcctInfoListPage(@JsonParam QueryParams qps,@JsonParam Coupons entity,
                                                         HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        List<Account> list = new ArrayList<Account>();
        Account account = new Account();
        try {
            qps.preDealSecurity(request);
            //优惠券适用门店为所有
            String all = "All";
            //优惠券适用门店的按门店等级
            String storeClass = "StoreClass";
            //优惠券适用门店的按门店类型
            String storeType = "StoreType";
            //优惠券适用门店的按门店按具体门店
            String specified = "Specified";
            if (all.equals(entity.getAppliceAccnt())) {
                list = accountService.queryByExamplePage(account);
                result.put("rows", list);
            } else if (storeClass.equals(entity.getAppliceAccnt())) {
                //模糊搜索
                qps.addFilter(new Filter("acctLevel", Filter.OP_OR_LIKE, entity.getStoreLevel()));
                list = accountService.queryByExamplePage(account);
                result.put("rows", list);
            } else if (storeType.equals(entity.getAppliceAccnt())) {
                //模糊搜索
                qps.addFilter(new Filter("subAcctType", Filter.OP_OR_LIKE, entity.getStoreType()));
                list = accountService.queryByExamplePage(account);
                result.put("rows", list);
                //具体门店
            } else if (specified.equals(entity.getAppliceAccnt()))
            {
                CoupStore coupStore = new CoupStore();
                coupStore.setCouponsId(entity.getId());
                List<CoupStore> coupStorelist = coupStoreService.queryByExamplePage(coupStore);
                result.put("rows", coupStorelist);
                //  通过区域下的所有门店
            } else
            {
                CoupOrg coupOrg = new CoupOrg();
                coupOrg.setCouponsId(entity.getId());
                List<CoupOrg> coupOrgList = coupOrgService.queryByExamplePage(coupOrg);
                result.put("rows", coupOrgList);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<SaleActivity>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 消费者直接领取优惠卷
     * @author: 汪大伟
     * @date: 2018/5/19
     * @param entity 优惠卷实体类
     */
    @RequestMapping(value = "/getCoupons")
    @ResponseBody
    public Map<String, Object> getCoupons(@JsonParam QueryParams qps,@JsonParam Coupons entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        //微信openId
        String openId = request.getParameter("openId");
        try {
            result = couponsService.getCoupons(entity, openId, request);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Coupons>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 消费者是否领取过该优惠卷领取优惠卷
     * @author: 汪大伟
     * @date: 2018/5/19
     */
    @RequestMapping(value = "/queryByCoupons")
    @ResponseBody
    public Map<String, Object> queryByCoupons(@JsonParam QueryParams qps,@JsonParam Coupons entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            CollectRecord collectRecord = new CollectRecord();
            collectRecord.setMobilePhone(entity.getMobilePhone());
            collectRecord.setCouponsId(entity.getId());
            collectRecord.setCorpid(entity.getCorpid());
            collectRecord.setLoginCorpId(entity.getLoginCorpId());
            List<CollectRecord> collectRecordList = collectRecordService.queryAll(collectRecord);
            result.put("result", collectRecordList.size());
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
        }
        return result;
    }

    /**
     * <p>根据id获取，优惠券品牌，系列，同步微信状态等</p>
     *
     * @author 汪大伟
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return map
     * @Date: 2018/5/19
     */
    @RequestMapping(value = "/getCouponsById")
    @ResponseBody
    public Map<String, Object> getCouponsById(@RequestBody Coupons entity, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            Coupons coupons = couponsService.getCouponsById(entity);
            result.put("success", true);
            result.put("result", coupons);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 编辑优惠券信息，优惠券品牌，系列，大转盘活动抽奖规则，同步微信状态等
     * @param couponsList
     * @Date: 2018/5/19
     * @author 汪大伟
     */
    @RequestMapping(value = "/updateCoupons")
    @ResponseBody
    public Map<String, Object> updateCoupons(@RequestBody List<Coupons> couponsList, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            couponsService.updateCoupons(couponsList);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>核销优惠券</p>
     * @author 汪大伟
     * @param entity
     * @date: 2018/5/25
     */
    @RequestMapping(value = "/clippingCoupons")
    @ResponseBody
    public Map<String, Object> clippingCoupons(@RequestBody CollectRecord entity, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            result = couponsService.useCoupons(entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>删除活动下的优惠券，以及品牌系列，优惠券的使用产品信息</p>
     * @author 汪大伟
     * @param entity
     * @date: 2018/6/6
     */
    @RequestMapping(value = "/deleteCoupons")
    @ResponseBody
    public Map<String, Object> deleteCoupons(@RequestBody Coupons entity, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            //优惠券可删除条件活动状态为未开始优惠券状态为新建，并且在优惠券领取记录表里面没有领取记录
            SaleActivity salAct = new SaleActivity();
            salAct.setId(entity.getActivityId());
            SaleActivity saleActivity = saleActivityService.queryById(salAct);
            CollectRecord collectRecord = new CollectRecord();
            collectRecord.setOnlyCountFlag(true);
            collectRecord.setCouponsId(entity.getId());
            List<CollectRecord> list = collectRecordService.queryByExamplePage(collectRecord);
            String state = "New";
            if (state.equals(saleActivity.getActivityState())
                    && list.get(0).getTotal() == 0 && "New".equals(entity.getCouponsStuts())) {
                couponsService.deleteCoupons(entity);
                result.put("success", true);
                result.put("result", "删除成功");
            } else {
                if (!state.equals(saleActivity.getActivityState())) {
                    result.put("result", "当前活动已开始，不能删除该优惠券");
                }
                if (list.get(0).getTotal() == 0 || !state.equals(entity.getCouponsStuts())) {
                    result.put("result", "当前优惠券已被领取，不能删除");
                }
                result.put("success", false);
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * pc端查看优惠券信息
     * @author 汪大伟
     * @date: 2018/6/19
     * @param entity
     */
    @RequestMapping(value = "/pcGetCoupon")
    @ResponseBody
    public Map<String, Object> pcGetCoupon(@JsonParam QueryParams qps,@JsonParam Coupons entity, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            //优惠券可删除条件活动状态为未开始优惠券状态为新建，并且在优惠券领取记录表里面没有领取记录
            List<CollectRecord> list = couponsService.pcGetCoupons(entity);
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 传入活动id，和客户电话号码，查看优惠券
     * @author 汪大伟
     * @param entity
     * @date: 2018/6/21
     */
    @RequestMapping(value = "/showCoupons")
    @ResponseBody
    public Map<String, Object> showCoupons(@JsonParam QueryParams qps,@JsonParam Coupons entity, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            List<Coupons> list = couponsService.showCoupons(entity);
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询一条记录,特殊需求
     */
    @RequestMapping(value = "/newQueryById")
    @ResponseBody
    public Map<String, Object> newQueryById(@RequestBody Coupons entity, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            // 未登录状态手动添加一个系统默认用户
            User user = userService.queryUserByCropId(entity.getCorpid());
            SecurityInterceptor.tempUser.set(user);
            Coupons coupons = couponsService.queryById(entity);
            //匿名标记
            coupons.setAnonymousFlag("Y");
            result.put("result", coupons);
            result.put("success", true);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 更新礼品状态为已使用
     * @author : LiQun
     * @date : 下午7:29 2018/5/2
     */

    @RequestMapping(value = "/presentStatusUpdate")
    @ResponseBody
    public Map<String, Object> insert(@RequestBody Present entity) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            couponsService.presentStatusUpdate(entity);
            entity.setCouponStatus("Used");
            result.put("result", entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 根据客户id查询其关联的礼品记录
     * @author : LiQun
     * @date : 下午7:40 2018/5/2
     */

    @RequestMapping(value = "/queryPresentsByAccountId")
    @ResponseBody
    public Map<String, Object> queryPresentsByAccountId(@JsonParam Present entity) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            List<Present> guidePresentList = couponsService.queryPresentsByAccountId(entity);
            result.put("rows", guidePresentList);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取优惠券二维码
     * @author 汪大伟
     * @param entity 优惠券
     * @return 二维码url
     */
    @RequestMapping(value = "/getCouponQrCode")
    @ResponseBody
    public Map<String, Object> getCouponQrCode(@JsonParam Coupons entity, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            String img = null;
            // 优惠券的id
            Long id = entity.getId();
            // 优惠券指向的url
            String url = entity.getCodeUrl();
            LinkRedisClient jedis = RedisUtil.getJedis();
            String key = "coupon_" + id;
            if (StringUtils.isNotBlank(url)) {
                try {
                    /*带参优惠券*/
                    jedis.hget(key, "param_coupon");
                    /*不带参优惠券*/
                    jedis.hget(key, "no_param_coupon");
                    int width = 200;
                    int height = 200;
                    QRCodeWriter writer = new QRCodeWriter();
                    BitMatrix m = writer.encode(url, BarcodeFormat.QR_CODE, width, height);
                    BufferedImage image = MatrixToImageWriter.toBufferedImage(m);
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    //将BufferedImage转成out输出流
                    ImageIO.write(image, "png", out);
                    img = Base64.encodeBase64String(out.toByteArray());

                } catch (Exception e) {
                    // TODO: handle exception
                    LCLogger.withException(e);
                }
            }
            result.put("result", img);
            result.put("success", true);
            RedisUtil.returnResource(jedis);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 会员卡查询
     * @author  lujiang
     * 10/9 2018
     */
    @RequestMapping(value = "/queryWxCard")
    @ResponseBody
    public Map<String, Object> queryWxCard(@JsonParam Coupons coupons ,HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            map = couponsService.queryWxCard(coupons);
        } catch (Exception e) {
            e.getMessage();
        }
        return map;
    }

    /**
     * 小程序领取优惠券
     * @author  lujiang
     */
    @RequestMapping(value = "/getWxCard")
    @ResponseBody
    public Map<String, Object> getWxCard(QueryParams qps, @RequestBody Coupons coupon, HttpSession session,
                                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            String  openId = coupon.getAttr1();
            if ("".equals(openId)) {
                map.put("msg", "openid不能为空");
                return map;
            }
            //获取优惠券list
            List<Coupons> coupons = coupon.getCouponsList();
            map = couponsService.getWxCard(coupons,openId);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            e.getMessage();
        }
        return map;
    }

    /**
     * 根据产品查询优惠券
     * @author  lujiang
     */
    @RequestMapping(value = "/productForCard")
    @ResponseBody
    public Map<String, Object> productForCard(@RequestBody Product product) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            product.setCorpid(product.getLoginCorpId());
            product = productService.queryById(product);
            map = couponsService.productForCard(product);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            e.getMessage();
        }
        return map;
    }

    /**
     * 小程序订单查询适用优惠券
     * @author: lujiang
     * @param entity
     */
    @RequestMapping(value = "/queryOrderCoupons")
    @ResponseBody
    public Map<String, Object> queryOrderCoupons(@RequestBody AppOrderInfo entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> map = new HashMap<>(16);
        try {
            map = couponsService.queryOrderCoupons(entity);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * @author 汪大伟
     * @param coupons
     * @return
     */
    @RequestMapping(value = "/queryBycenes")
    @ResponseBody
    public Map<String, Object> queryBycenes(@RequestBody Coupons coupons) {
        Map<String, Object> map = new HashMap<>(8);
        try {
            LinkRedisClient j = RedisUtil.getJedis();
            String redisKey = "MP_QRCODE:" + coupons.getId();
            // 将数据取出来
            String userId = j.hget(redisKey, "userId");
            String type = j.hget(redisKey, "type");
            String id = j.hget(redisKey, "id");
            String source = j.hget(redisKey, "source");
            String activityType = j.hget(redisKey, "activityType");
            map.put("userId",userId);
            map.put("type",type);
            map.put("id",id);
            map.put("source",source);
            map.put("activityType",activityType);
            map.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            map.put("success", false);
            map.put("msg", e.getMessage());
        }

        return map;
    }

    /**
     * 扫码核销优惠券-导购扫描消费者使用的二维码
     * @author 汪大伟
     * @param entity
     * @return
     */
    @RequestMapping(value = "/scanCodeUseCoup")
    @ResponseBody
    public Map<String, Object> scanCodeUseCoup(@RequestBody Coupons entity) {
        Map<String,Object> map = new HashMap<>(8);
        try {
            entity = couponsService.scanCodeUseCoup(entity);
            map.put("success", true);
            map.put("newRow", entity);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            e.getMessage();
        }
        return map;
    }

    /**
     * 唐健
     * 根据活动查询活动下的优惠券
     * @param entity
     * @return
     */
    @RequestMapping(value = "/queryCouponsByActivityId")
    @ResponseBody
    public Map<String, Object> queryCouponsByActivityId(@JsonParam Coupons entity) {
        Map<String, Object> map = new HashMap<>(8);
        try {
            entity.setPageFlag(false);
            List<Coupons> list = couponsService.queryByExamplePage(entity);
            for (Coupons c : list) {
                c.setAnonymousFlag("Y");
            }
            map.put("success", true);
            map.put("rows", list);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            e.getMessage();
        }
        return map;
    }

    /**
     * H5页面领取张优惠券(支持客户一键领取多张优惠券)
     * @author 汪大伟
     * @param entity
     * @return
     */
    @RequestMapping(value = "/receiveCoupons")
    @ResponseBody
    public Map<String, Object> receiveCoupons(@RequestBody SaleActivity entity,HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(8);
        try {
            String openId = request.getParameter("openId");
            Long corpid = entity.getCorpid();
            List<Coupons> couponsList = entity.getCouponsList();
            String mobilePhone = entity.getPhoneNum();
            map = couponsService.receiveCoupons(couponsList,mobilePhone,openId,corpid);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            e.getMessage();
        }
        return map;
    }

    @RequestMapping(value = "/queryMPOrderCoupons")
    @ResponseBody
    public Map<String, Object> queryMPOrderCoupons(@RequestBody AppOrderInfo entity, HttpSession session,
                                                   HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(4);
        try {
            map = couponsService.queryMPOrderCoupons(entity);
            map.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * 根据电话号码判断判断客户是否已经领券
     * @author 汪大伟
     * @param saleActivity
     * @return
     */
    @RequestMapping(value = "/checkCoupon")
    @ResponseBody
        public Map<String, Object> checkCoupon(@RequestBody SaleActivity saleActivity, HttpSession session,
                                                   HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(4);
        String mobilePhone = saleActivity.getPhoneNum();
        Long corpid = saleActivity.getCorpid();
        List<Coupons> couponsList = saleActivity.getCouponsList();
        try {
            couponsList = couponsService.checkCoupon(couponsList,mobilePhone,corpid);
            map.put("success",true);
            map.put("rows",couponsList);
        } catch (Exception e) {
            LCLogger.withException(e);
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * 保存红包活动
     * @author 路江
     * @param coupons
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/createdRedPackActivity")
    @ResponseBody
    public Map<String, Object> createdRedPackActivity(@RequestBody Coupons coupons, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(4);
        try {
            map = couponsService.createdRedPackActivity(coupons);
            map.put("success",true);
            map.put("rows",coupons);
        } catch (Exception e) {
            LCLogger.withException(e);
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * 查询红包活动
     * @author 路江
     * @param coupons
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryRedPackActivity")
    @ResponseBody
    public Map<String, Object> queryRedPackActivity(@RequestBody Coupons coupons, HttpSession session,
                                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(4);
        try {
            map = couponsService.queryRedPackActivity(coupons);
            map.put("success",true);
        } catch (Exception e) {
            LCLogger.withException(e);
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * 会员-查询优惠券详细信息
     *
     * @author lijie
     * @param entity
     * @date   2019/8/09 12:00
     */
    @RequestMapping("/queryOtherById")
    @ResponseBody
    public Map<String, Object> queryOtherById(@RequestBody Coupons entity, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            Coupons record = couponsService.queryById(entity);
            result.put("result", record);
            result.put("success", true);
        } catch (Exception var9) {
            result.put("success", false);
            result.put("result", var9.getMessage());
        }

        return result;
    }
}
